import pygame
from pygame.locals import *
    
class SimpleUI:
    
    RED = (255,0,0)
    BLUE = (0,0,255)

    def __init__(self):
        # Initialize PyGame
        pygame.init()
        pygame.display.set_caption('Paint')
        self.screen = pygame.display.set_mode((640,480))
        self.screen.fill((255,255,255))
        self.button1 = Rect(20, 20, 100, 50)
        self.button2 = Rect(20, 90, 100, 50)
        self.button2 = Rect(20, 50, 100, 50)
        self.colorArea = Rect(140, 20, 400, 400)
        self.selectedColor = SimpleUI.BLUE
        
    def run(self):
        # Run the event loop
        self.loop()
        # Close the Pygame window
        pygame.quit()
    
    def loop(self):
        exit = False
        while not exit:
            exit = self.handleEvents()
            self.draw()

    def handleEvents(self):
            exit = False
            for event in pygame.event.get():
                if event.type == QUIT:
                    exit = True
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        exit = True
                elif event.type == MOUSEBUTTONDOWN:
                    self.handleMouseDown(pygame.mouse.get_pos())
            return exit

    def handleMouseDown(self, pos):
        x = pos[0]
        y = pos[1]
        print (x,y)
        if (self.button1.collidepoint(x, y)):
            self.selectedColor = SimpleUI.BLUE
            print ("BLUE")
        elif (self.button2.collidepoint(x, y)):
            self.selectedColor = SimpleUI.RED
            print ("RED")

            
    def draw(self):
        pygame.draw.rect(
            self.screen,
            SimpleUI.BLUE,
            self.button1,
            )
        pygame.draw.rect(
            self.screen,
            SimpleUI.RED,
            self.button2,
            )
        pygame.draw.rect(
            self.screen,
            self.selectedColor,
            self.colorArea,
            )
        pygame.display.update()
            
# Start the game
SimpleUI().run()
